package com.zhao.util

/**
 * Description: 参数解析器<br/>
 * Copyright (c) ，2021 ， 赵 <br/>
 * This program is protected by copyright laws. <br/>
 * Date： 2021/1/5 15:55
 *
 * @author 柒柒
 * @version : 1.0
 */

object ParamParserUtil {

  /**
   * args转操作map
   * @param args
   * @return
   */
  def argsToOptionMap(args: Array[String]): Map[String, String] = {
    def nextOption(argList: List[String], map: Map[String, String]): Map[String, String] = {
      //select Arg from --Arg
      val pattern = "--([\\w\\._]+)".r
      //select Arg from -Arg
      val patternSwitch = "-([\\w\\._]+)".r
      argList match{
        case Nil => map
        case pattern(opt) :: value :: tail => nextOption(tail, map ++ Map(opt -> value))
        case patternSwitch(opt) :: tail => nextOption(tail, map ++ Map(opt -> null))
        case string :: Nil => map ++ Map(string -> null)
        case option :: tail => {
          println("Unknown option:" + option)
          sys.exit(1)
        }
      }
    }
    //遍历调用
    nextOption(args.toList, Map())
  }
}










